package com.mayajie;

import java.util.Scanner;

/**
 * @author mayajie
 * @date 2021/10/22
 */
public class BinarySearch {
    public static void main(String[] args) {
        int[] ints = {2, 3, 4, 5, 6, 7, 8, 9};
//        for(int i:ints) {
//            search(ints, i);
//        }
        while (true) {
            search(ints, new Scanner(System.in).nextInt());
        }
    }

    /**
     * [1][2][3][4][5][6][7][8][9]
     *  0  1  2  3  4  5  6  7  8
     *  0  1  2  3     5  6  7  8
     *  0  1     3     5  6     8
     *  0              5
     * @param ints
     * @param target
     */
    public static void search(int[] ints, int target) {
        int start = 0;
        int end = ints.length;
        while (true) {
            int center = start + (end-start)/2;
            int temp = ints[center];
            System.out.println("二分比较数：" + temp);
            if (temp == target) {
                System.out.println("位置：" + center);
                break;
            } else if (temp > target) {
                end = center;
            } else {
                start = center+1;
            }
            if (start == end) {
                System.out.println("未找到，退出！");
                break;
            }
        }
    }

    public static int s(int[] ints, int target) {
        int start = 0;
        int end = ints.length - 1;
        while (start <= end) {
            int middle = (start + end) >>> 1;
            int temp = ints[middle];
            if (temp == target) {
                return middle;
            } else if (temp > target) {
                end = middle - 1;
            } else {
                start = middle + 1;
            }
        }
        return -1;
    }
}
